package com.xunji.standard.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xunji.standard.common.Constant;
import com.xunji.standard.entity.CaseUploadInfo;
import com.xunji.standard.mapper.CaseUploadInfoMapper;
import com.xunji.standard.service.CaseUploadInfoService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: xuqiang_imyours
 * @Date: 2025/6/23 11:31
 * @Description:
 **/
@Service
public class CaseUploadInfoImpl extends ServiceImpl<CaseUploadInfoMapper, CaseUploadInfo> implements CaseUploadInfoService {
    @Override
    public Long add(String caseId, String uploadFilePath) {
        CaseUploadInfo info = new CaseUploadInfo();
        info.setCaseId(caseId);
        info.setIsSend(Constant.S_ZERO);
        info.setStatus(Constant.I_ONE); // 文件解密状态
        info.setUploadPath(uploadFilePath);
        info.setCreatedTime(LocalDateTime.now());
        this.save(info);
        return info.getId();
    }

    @Override
    public void updateInfoById(Long infoId, String copyPath) {
        UpdateWrapper<CaseUploadInfo> qw = new UpdateWrapper<>();
        qw.set("copy_path", copyPath).eq("id", infoId);
        this.update(qw);
    }

    @Override
    public List<CaseUploadInfo> getListByCaseId(String caseId) {
        QueryWrapper<CaseUploadInfo> qw = new QueryWrapper<>();
        qw.eq("case_id", caseId).orderByDesc("id");
        return this.baseMapper.selectList(qw);
    }

    @Override
    public CaseUploadInfo getById(Long id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public String getInfoByCaseId(String caseId) {
        List<CaseUploadInfo> listByCaseId = getListByCaseId(caseId);
        if (listByCaseId != null && !listByCaseId.isEmpty()) {
            return listByCaseId.get(0).getUploadPath();
        }
        return "";
    }

    @Override
    public void updateStatusById(Long infoId, int status) {
        UpdateWrapper<CaseUploadInfo> qw = new UpdateWrapper<>();
        qw.set("status", status).eq("id", infoId);
        this.update(qw);
    }

    @Override
    public void deleteByCaseId(String caseId) {
        QueryWrapper<CaseUploadInfo> qw = new QueryWrapper<>();
        qw.eq("case_id", caseId);
        this.baseMapper.delete(qw);
    }

    @Override
    public void isRead(Long id) {
        CaseUploadInfo uploadInfo = this.getById(id);
        if (uploadInfo != null) {
            uploadInfo.setFirstRead(Constant.I_ONE);
            this.updateById(uploadInfo);
        }
    }

}
